#-- coding: utf-8 -*-
import utils, sys
lstDir = utils.listdir("tgame/script/")
print "lstDir = %s" % str(lstDir)
sys.path.extend(lstDir)

import tgame_net_handler, t_cmd
import tgame_room_logic
import iapi
import tgame_ready
import tgame_menu
tgame_API = None
GAMEID = -1

GAME_MODE = None
UID = None
GAME_ID = None
#游戏的入口函数
def start_from_init(cgame_id, game_mode, uid):
	#初始化API的版本
	global tgame_API
	if tgame_API is None:
		tgame_API = iapi.API_1_0()
	#初始化游戏逻辑和ui，并显示准备界面
	global UID,GAME_ID,GAME_MODE
	GAME_MODE = game_mode
	UID = uid
	GAME_ID = cgame_id
	start_game(game_mode, uid)

	tgame_room_logic.init(GAME_MODE)
	#注册关键回调函数
	tgame_API.register_callback(
		cgame_id,
		tgame_room_logic.inst.logic_frame, tgame_room_logic.inst.render_frame, None,
		tgame_room_logic.inst.on_key_down, tgame_room_logic.inst.on_mouse_msg, None, None, None
	)

	#初始化网络消息映射表
	tgame_net_handler.init()
	#注册网络消息定义和回调函数
	tgame_API.register_game_room_msgdefine_and_callback(t_cmd.cmd_msg, tgame_net_handler.MSG_MAP)


#初始化游戏内的对象实例和ui实例
def start_game(iMode, uid):
	#初始化游戏音乐音效
	import tgame_sound
	tgame_sound.init()
	#初始化准备界面逻辑
	tgame_room_logic.init(iMode)
	tgame_room_logic.inst.init_myself(uid)
	#初始化游戏主逻辑
	import tgame_mapmgr
	tgame_mapmgr.init()	
	#初始化准备界面ui
	tgame_ready.init()
	tgame_ready.inst.show()

	#import tgame_ui_wait
	#tgame_ui_wait.init()
	#tgame_ui_wait.inst.show()
	#初始化游戏区域管理逻辑
	#import tgame_picmgr
	#tgame_picmgr.init()
	#初始化游戏中ui
	#import tgame_seedbank
	#tgame_seedbank.init()
	#import tgame_timebar
	#tgame_timebar.init()
	#初始化游戏结算ui
	#import tgame_result
	#tgame_result.init()
	#初始化游戏发送消息逻辑
	import tgame_command
	tgame_command.init()
	#初始化游戏成就
	import tgame_achieve
	tgame_achieve.init()
	import tgame_mapevent
	tgame_mapevent.init()


#释放游戏内对象
def back_to_hall():
	print "destroy games res!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	#销毁游戏准备界面逻辑和ui
	tgame_room_logic.destroy()
	tgame_ready.destroy()
	#销毁游戏主逻辑
	import tgame_mapmgr
	tgame_mapmgr.destroy()
	# 声音
	import tgame_sound
	tgame_sound.destroy()	
	#销毁游戏中ui和结算ui
	#import tgame_seedbank
	#tgame_seedbank.destroy()
	#import tgame_timebar
	#tgame_timebar.destroy()
	#import tgame_result
	#tgame_result.destroy()
	#销毁游戏发送逻辑
	#import tgame_command
	#tgame_command.destroy()
	#销毁游戏区域管理逻辑
	#import tgame_picmgr
	#tgame_picmgr.destroy()
	#清理游戏音乐音效
	#import tgame_sound
	#tgame_sound.destroy()
	#销毁游戏成就
	#import tgame_achieve
	#tgame_achieve.destroy()
	#销毁API对象
	global tgame_API
	tgame_API = None
	#import tgame_mapevent
	#tgame_mapevent.destroy()
	
